<?php
class indexController extends RPHP_Controller
{
    public function v()
    {
        wlog("ccccc");die;
        echo "version:".APP_VERSION;
    }

    public function hook()
    {

        $json = file_get_contents("php://input");
        $data = json_decode($json,true);
        if (isset($data['ref']) && $data['total_commits_count']>0) {
            $res = PHP_EOL."pull start ---------------------------------------------".PHP_EOL;
            $res .= shell_exec("cd /www/wwwroot/xihai && git pull 2<&1 ");
            $res_log = '------------------------------------------------------------'.PHP_EOL;
            $res_log .= $data['user_name'] . ' 在' . date('Y-m-d H:i:s') . '向' . $data['repository']['name'] . '项目的' . $data['ref'] . '分支push了' . $data['total_commits_count'] . '个commit：'.$data['commits']['message'];
            $res_log .= $res.PHP_EOL;
            $res_log .= "pull end -----------------------------------------------------".PHP_EOL;
            wlog($res_log);
            //file_put_contents("/www/wwwroot/xihai/webhook/".date('Y-m-d',time()).".txt", $res_log, FILE_APPEND);//写入日志到log文件中
        }
    }
    public function index()
    {

        if ($_GET['code'] != "") {
            $this->view->alert = $this->c['error'][$_GET['code']];
        }

        if ($_GET['account'] != "") {
            $this->view->us = $_GET['account'];
        }

        $this->display();

    }

    public function dosignin()
    {
        $rs['code'] = 900;
        if ($_POST['act'] == "Signin") {

            //检查验证码
            session_start();

            if ($_SESSION['verify_rand'] != "" && strlen($_SESSION['verify_rand']) == 4) {

                if (strtolower($_POST['verifycode']) == $_SESSION['verify_rand']) {
                    $_SESSION['verify_rand'] = "";

                    $m  = $this->model;
                    $rs = $m->doSignin($_POST);
                    if ($rs['code'] == 200 && $rs['data'] != "") {

                        setcookie($this->c['cookie']['user'], Rfc::encode(serialize($rs['data'])), 0, $this->c['cookie']['path'], $this->c['cookie']['host']);
                        header("location: /manage");
                        exit;
                    }

                } else {
                    $rs['code'] = 300;
                }

            } else {
                $rs['code'] = 301;
            }

        }

        $url = "/?code=" . $rs['code'] . "&account=" . $_POST['signin_account'];
        header("location: " . $url);

    }

}
